﻿using System.Linq;
using System.Threading.Tasks;

using Hang.Blog.DomainPlus.Dtos;

using Microsoft.EntityFrameworkCore;

using Volo.Abp.Application.Dtos;

namespace Hang.Blog.DomainPlus.EntityframeworkExtension {
    public static class IQueryableExtension {
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static async Task<PagedResultDto<T>> Page<T>(this IQueryable<T> query, PageDto obj) where T : class {
            var count = await query.CountAsync();
            var data = await query.PageBy((obj.PageIndex - 1) * obj.PageSize, obj.PageSize).Take(obj.PageSize).ToListAsync();
            var res = new PagedResultDto<T>() {
                Items = data,
                TotalCount = count
            };
            return res;
        }
    }
}
